/*
 *  Copyright 2007 Blandware (http://www.blandware.com)
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.blandware.atleap.webapp.struts;

import org.apache.struts.tiles.xmlDefinition.XmlAttribute;

import java.util.List;

/**
 * <p>During parsing, represents list attribute which may be a container.</p>
 * <p>Initial code is taken from XmlListAttribute class.</p>
 * <p><a href="ContainerXmlListAttribute.java.html"><i>View source</i></a></p>
 *
 * @author Roman Puchkovskiy <a href="mailto:roman.puchkovskiy@blandware.com">
 *         &lt;roman.puchkovskiy@blandware.com&gt;</a>
 * @version $Revision: 1.2 $ $Date: 2007/04/09 14:06:44 $
 */
public class ContainerXmlListAttribute extends XmlAttribute
        implements PossibleContainer {

    /**
     * Whether this is a container (i.e. can contain components)
     */
    protected boolean container;

    /**
     * Description
     */
    protected String description;

    /**
     * List.
     * We declare a List to avoid cast.
     * Parent "value" property points to the same list.
     */
    protected List list;

    /**
     * Constructor.
     */
    public ContainerXmlListAttribute() {
        list = new ContainerList();
        setValue(list);
    }

    /**
     * Constructor.
     *
     * @param name  Name.
     * @param value List.
     */
    public ContainerXmlListAttribute(String name, List value) {
        super(name, value);
        list = value;
    }

    /**
     * Add an element in list.
     * We use a property to avoid rewriting a new class.
     *
     * @param element XmlAttribute to add.
     */
    public void add(XmlAttribute element) {
        list.add(element.getValue());
    }

    /**
     * Add an element in list.
     *
     * @param value Object to add.
     */
    public void add(Object value) {
        //list.add( value );
        // To correct a bug in digester, we need to check the object type
        // Digester doesn't call correct method according to object type ;-(
        if (value instanceof XmlAttribute) {
            add((XmlAttribute) value);
        } else {
            list.add(value);
        }
    }

    /**
     * Add an element in list.
     *
     * @param value Object to add.
     */
    public void addObject(Object value ) {
        list.add(value);
    }

    /**
     * Returns whether this is a container.
     *
     * @return true if this is a container
     */
    public boolean isContainer() {
        return container;
    }

    /**
     * Sets whether this is a container.
     *
     * @param container value to set
     */
    public void setContainer(boolean container) {
        this.container = container;
        if (list instanceof PossibleContainer) {
            ((PossibleContainer) list).setContainer(container);
        }
    }

    /**
     * Returns description.
     *
     * @return description
     */
    public String getDescription() {
        return description;
    }

    /**
     * Sets description.
     *
     * @param description description to set
     */
    public void setDescription(String description) {
        this.description = description;
        if (list instanceof PossibleContainer) {
            ((PossibleContainer) list).setDescription(description);
        }
    }
}
